package br.edu.univas.si.engs.view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import br.edu.univas.si.engs.controller.ControllerCadastro;
import br.edu.univas.si.engs.model.ClientTO;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class DialogCadastrarCliente extends JDialog {

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel ConsultarClientePanel;
	private JLabel NomeConsultarLabel;
	private JTextField NomeConsultarField;
	private JButton NewCadastroButton;
	private JScrollPane jScrollPane1;
	private JTable CadastroTable;
	private JButton ExcluirCadastroButton;
	private JButton CancelButton;
	private JButton EditarCadastroButton;
	private JButton ConsultarButton;
	private DefaultTableModel tableModel;
	DialogClienteDados dialogClienteDados;

	ControllerCadastro cc = new ControllerCadastro();

	/**
	 * Auto-generated main method to display this JDialog
	 */

	public DialogCadastrarCliente(JFrame owner, String title, boolean modal) {
		super(owner, title, modal);
		initGUI();
		// setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);
	}

	private void initGUI() {

		try {
			GroupLayout thisLayout = new GroupLayout(
					(JComponent) getContentPane());
			getContentPane().setLayout(thisLayout);
			{
				ConsultarClientePanel = new JPanel();
				GroupLayout ConsultarClientePanelLayout = new GroupLayout(
						(JComponent) ConsultarClientePanel);
				ConsultarClientePanel.setLayout(ConsultarClientePanelLayout);
				ConsultarClientePanel.setBorder(BorderFactory
						.createCompoundBorder(null, null));
				{
					NomeConsultarLabel = new JLabel();
					NomeConsultarLabel.setText("Nome:");
				}
				{
					NomeConsultarField = new JTextField();
				}
				{
					ConsultarButton = new JButton();
					ConsultarButton.setText("Consultar");
					ConsultarButton.setIcon(new ImageIcon(getClass()
							.getClassLoader().getResource(
									"images/pesquisar.png")));
					ConsultarButton.addActionListener(new ActionListener() {

						@Override
						public void actionPerformed(ActionEvent e) {
							getListar();

						}
					});
				}
				ConsultarClientePanelLayout
						.setVerticalGroup(ConsultarClientePanelLayout
								.createSequentialGroup()
								.addContainerGap(17, 17)
								.addGroup(
										ConsultarClientePanelLayout
												.createParallelGroup(
														GroupLayout.Alignment.BASELINE)
												.addComponent(
														NomeConsultarField,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														ConsultarButton,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														21,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														NomeConsultarLabel,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addGap(16)
								.addComponent(getJScrollPane1(),
										GroupLayout.PREFERRED_SIZE, 228,
										GroupLayout.PREFERRED_SIZE));
				ConsultarClientePanelLayout
						.setHorizontalGroup(ConsultarClientePanelLayout
								.createSequentialGroup()
								.addContainerGap(10, 10)
								.addComponent(NomeConsultarLabel,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										ConsultarClientePanelLayout
												.createParallelGroup()
												.addGroup(
														ConsultarClientePanelLayout
																.createSequentialGroup()
																.addComponent(
																		NomeConsultarField,
																		GroupLayout.PREFERRED_SIZE,
																		359,
																		GroupLayout.PREFERRED_SIZE)
																.addGap(146)
																.addComponent(
																		ConsultarButton,
																		GroupLayout.PREFERRED_SIZE,
																		107,
																		GroupLayout.PREFERRED_SIZE))
												.addComponent(
														getJScrollPane1(),
														GroupLayout.Alignment.LEADING,
														GroupLayout.PREFERRED_SIZE,
														612,
														GroupLayout.PREFERRED_SIZE))
								.addContainerGap(48, 48));
			}
			{
				NewCadastroButton = new JButton();
				NewCadastroButton.setText("Cadastrar");
				NewCadastroButton.setIcon(new ImageIcon(getClass()
						.getClassLoader().getResource("images/incluir.png")));
				NewCadastroButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						DialogClienteDados m = new DialogClienteDados();
						m.setVisible(true);

					}
				});
			}
			{
				EditarCadastroButton = new JButton();
				EditarCadastroButton.setText("Editar");
				EditarCadastroButton.setIcon(new ImageIcon(getClass()
						.getClassLoader().getResource("images/editar.png")));
				EditarCadastroButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {

						getEditar();

					}
				});
			}
			{
				ExcluirCadastroButton = new JButton();
				ExcluirCadastroButton.setText("Excluir");
				ExcluirCadastroButton.setIcon(new ImageIcon(getClass()
						.getClassLoader().getResource("images/excluir.png")));
				ExcluirCadastroButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						getExcluir();
						getListar();

					}
				});
			}
			{
				CancelButton = new JButton();
				CancelButton.setText("Cancelar");
				CancelButton.setIcon(new ImageIcon(getClass().getClassLoader()
						.getResource("images/cancel.png")));
				CancelButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						dispose();

					}
				});
			}
			thisLayout.setVerticalGroup(thisLayout
					.createSequentialGroup()
					.addComponent(ConsultarClientePanel,
							GroupLayout.PREFERRED_SIZE, 282,
							GroupLayout.PREFERRED_SIZE)
					.addGap(27)
					.addGroup(
							thisLayout
									.createParallelGroup(
											GroupLayout.Alignment.BASELINE)
									.addComponent(NewCadastroButton,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE, 21,
											GroupLayout.PREFERRED_SIZE)
									.addComponent(EditarCadastroButton,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE, 21,
											GroupLayout.PREFERRED_SIZE)
									.addComponent(ExcluirCadastroButton,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE, 21,
											GroupLayout.PREFERRED_SIZE)
									.addComponent(CancelButton,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE, 21,
											GroupLayout.PREFERRED_SIZE))
					.addContainerGap(18, 18));
			thisLayout.setHorizontalGroup(thisLayout
					.createParallelGroup()
					.addComponent(ConsultarClientePanel,
							GroupLayout.Alignment.LEADING, 0, 705,
							Short.MAX_VALUE)
					.addGroup(
							GroupLayout.Alignment.LEADING,
							thisLayout
									.createSequentialGroup()
									.addGap(34)
									.addComponent(NewCadastroButton,
											GroupLayout.PREFERRED_SIZE, 144,
											GroupLayout.PREFERRED_SIZE)
									.addGap(17)
									.addComponent(EditarCadastroButton,
											GroupLayout.PREFERRED_SIZE, 108,
											GroupLayout.PREFERRED_SIZE)
									.addGap(15)
									.addComponent(ExcluirCadastroButton,
											GroupLayout.PREFERRED_SIZE, 101,
											GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addComponent(CancelButton,
											GroupLayout.PREFERRED_SIZE, 109,
											GroupLayout.PREFERRED_SIZE)
									.addContainerGap(159, Short.MAX_VALUE)));
			this.setSize(713, 382);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private DefaultTableModel getTableModel() {
		if (tableModel == null) {
			tableModel = new DefaultTableModel();
			tableModel
					.setColumnIdentifiers(new String[] { "ID", "Nome", "CPF",
							"RG", "Telefone", "Email", "Celular", "Dat.Nasc",
							"Dat.Cad" });

		}
		return tableModel;
	}

	public void getListar() {
		getTableModel().setRowCount(0);

		ArrayList<ClientTO> pp = cc.ListarCliente(NomeConsultarField.getText());

		for (ClientTO cliente : pp) {

			String[] newLine = new String[] { String.valueOf(cliente.getId()),
					cliente.getNome(), cliente.getCpf(), cliente.getRg(),
					cliente.getTelefone(), cliente.getEmail(),
					cliente.getCelular(), String.valueOf(cliente.getDataCad()),
					String.valueOf(cliente.getDataNasc())

			};
			getTableModel().addRow(newLine);

		}

	}

	private JTable getCadastroTable() {
		if (CadastroTable == null) {
			CadastroTable = new JTable(getTableModel());
		}
		return CadastroTable;
	}

	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setViewportView(getCadastroTable());
		}
		return jScrollPane1;
	}

	public int getSelecionarTable() {

		if (getCadastroTable().getSelectedRow() >= 0) {
			int num = getCadastroTable().getSelectedRow();
			getCadastroTable().getValueAt(num, 1);
			int cod = Integer.parseInt((String) getCadastroTable().getValueAt(
					num, 0));
			// System.out.println("Linha " +cod+ " foi selecionada.");

			return cod;
		}
		return -1;

	}

	public void getEditar() {

		if (getSelecionarTable() > 0) {
			System.out.println("getEditar()" + getSelecionarTable());
			ClientTO clientes = cc.editarCliente(getSelecionarTable());
			DialogClienteDados aditarDados = new DialogClienteDados();
			aditarDados.getEditarDados(clientes);
			aditarDados.setVisible(true);

		} else
			JOptionPane.showMessageDialog(null, "Cliente nao encontrado");

	}

	public void getExcluir() {
		int linha=getSelecionarTable();
		if (linha > 0) {
			System.out.println("getEditar();;;;;;;;" + linha);
			cc.excluirCliente(linha);

		} else
			JOptionPane.showMessageDialog(null, "Cliente nao encontrado");

	}

}
